package com.winit.core.configuration;

import com.winit.common.util.Constants;
import com.winit.common.util.FileUtil;
import com.winit.common.util.JSONFileUtil;
import com.winit.core.configuration.util.ConfigRepositoryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by wangjian on 16/8/1.
 *
 * 注:本方法中的projectName均为工程的configProjectName
 *
 */
public class ConfigRepository {

    private static final Logger logger = LoggerFactory.getLogger(ConfigRepository.class);

    /**
     * 本地仓库根路径
     * /usr/local/Beetle/svnResource/config
     */
    private String rootPath;

    public ConfigRepository(String rootPath){
        this.rootPath = rootPath;
    }

    /**
     *
     * @param projectName
     * @param envName
     * @param configPath 配置文件相对工程根目录路径
     * @return
     */
    public String getConfigURI(String projectName,String envName,String configPath) throws Exception{
        logger.info("ConfigRepository:获取环境配置文件路径,projectName="+projectName+",envName="+envName+",configPath="+configPath);
        if(StringUtils.isEmpty(projectName) || StringUtils.isEmpty(envName) || StringUtils.isEmpty(configPath)){
            String desc = "获取环境配置文件路径,参数为空.projectName="+projectName+",envName="+envName+",configPath="+configPath;
            logger.error("ConfigRepository:"+desc);
            throw new Exception(desc);
        }
        StringBuffer path = new StringBuffer();
        path.append(ConfigRepositoryUtil.getConfigEnvPath(this.rootPath,envName)).append("/").append(projectName).append("/").append(configPath);
        return path.toString();
    }

    public boolean isEnvExist(String envName){
        StringBuffer path = new StringBuffer();
        path.append(this.rootPath).append("/").append(ConfigRepositoryUtil.getEnv(envName)).append("/").append(envName);
        File file = new File(path.toString());
        return file.exists();
    }

    /**
     * 配置文件是否存在
     * @param projectName
     * @param envName
     * @param configPath
     * @return
     */
    public boolean isConfigFileExists(String projectName,String envName,String configPath) throws Exception{
        String path = getConfigURI(projectName, envName, configPath);
        File file = new File(path);
        return file.exists();
    }

    /**
     * 获取本地仓库中指定配置文件内容
     * @param projectName
     * @param envName
     * @param configPath
     * @return
     */
    public String getFileContent(String projectName,String envName,String configPath) throws Exception{
        String path = getConfigURI(projectName, envName, configPath);
        File file = new File(path);
        if(!file.exists()){
            logger.error("ConfigRepository:获取文件内容,文件不存在.file="+file.getPath());
            throw new Exception("获取文件内容,文件不存在.file="+file.getPath());
        }
        if(file.isDirectory()){
            logger.error("ConfigRepository:获取文件内容,传入文件为文件夹.file="+file.getPath());
            throw new Exception("获取文件内容,传入文件为文件夹.file="+file.getPath());
        }

        long length = file.length();
        if(length > 104857600){
            logger.error("ConfigRepository:获取文件内容,文件超过100k.file="+file.getPath()+",length="+length);
            throw new Exception("获取文件内容,文件超过100k.file="+file.getPath()+",length="+length);
        }

        String content = JSONFileUtil.getFileString(file.getAbsolutePath(), true);
        return content;
    }

    /**
     * 获取config本地仓库下环境列表
     * @return
     */
    public List<String> getConfigEnvList(){
        Set<String> set = new TreeSet<String>();

        File dir = new File(this.rootPath);
        List<File> fileList = FileUtil.getFileByLevel(dir,2);
        for (File file : fileList){
            if(file.isDirectory()){
                set.add(file.getName());
            }
        }
        List<String> list = new ArrayList<String>();
        List<String> temp = new ArrayList<String>();
        for(String name : set){
            if(name.startsWith(Constants.USER_ROLE_OPS)){
                temp.add(name);
                continue;
            }
            list.add(name);
        }
        if(temp.size() > 0){
            list.addAll(temp);
        }
        return list;
    }
}
